﻿/*字符串助手类
 */

namespace LSLib.StringEx
{
    using System;
    using System.Text;

    public static class StringHelper
    {
        /// <summary>字符串线
        /// </summary>
        /// <param name="mark">指定字符</param>
        /// <param name="length">该字符出现的次数</param>
        public static string StringLine(char mark, int count)
        {
            return new string(mark, count);
        }
        /// <summary>字符串线
        /// </summary>
        /// <param name="mark">指定字符</param>
        /// <param name="length">该字符出现的次数</param>
        public static string StringLine(string mark, int count)
        {
            if (count < 0) throw new ArgumentOutOfRangeException("count", count, "参数 count 必须为非负值。");

            StringBuilder result = new StringBuilder(count);
            for (int i = 0; i < count; i++)
            {
                result.Append(mark);
            }
            return result.ToString();
        }

        /// <summary>删除开头字符
        /// </summary>
        /// <param name="oldValue">待删除的字符串</param>
        /// <param name="count">要删除的字符数</param>
        public static string RemoveStarts(string oldValue, int count)
        {
            int length = oldValue.Length;

            if (count < 0 || count > length) throw new ArgumentOutOfRangeException("count", count, "参数 count 必须小于或等于 oldValue 的长度，且必须为非负值。");
            if (count == length) return string.Empty;

            return oldValue.Remove(0, count);
        }

        /// <summary>删除末位字符
        /// </summary>
        /// <param name="oldValue">待删除的字符串</param>
        /// <param name="count">要删除的字符数</param>
        public static string RemoveEnds(string oldValue, int count)
        {
            int length = oldValue.Length;

            if (count < 0 || count > length) throw new ArgumentOutOfRangeException("count", count, "参数 count 必须小于或等于 oldValue 的长度，且必须为非负值。");
            if (count == length) return string.Empty;

            return oldValue.Remove(length - count);
        }

        /// <summary>字符交换
        /// </summary>
        /// <param name="oldValue">待处理的字符串</param>
        /// <param name="pos1">位置1,从1开始计算</param>
        /// <param name="pos2">位置2,从1开始计算</param>
        /// <returns>交换后的字符串</returns>
        public static string SwapChar(string oldValue, int pos1, int pos2)
        {
            if (pos1 < 0 || pos1 > oldValue.Length) throw new ArgumentOutOfRangeException("pos1", pos1, "参数 pos1 必须小于或等于 oldValue 的长度，且必须为非负值。");
            if (pos2 < 0 || pos2 > oldValue.Length) throw new ArgumentOutOfRangeException("pos2", pos2, "参数 pos2 必须小于或等于 oldValue 的长度，且必须为非负值。");
            if (pos1 == pos2) return oldValue;

            int index1 = pos1 - 1, index2 = pos2 - 1;
            char[] result = oldValue.ToCharArray();
            result[index1] = oldValue[index2];
            result[index2] = oldValue[index1];
            return new string(result);
        }

        /// <summary>查找字符串，并返回区间内容
        /// </summary>
        /// <param name="sourceString">待查找的字符串</param>
        /// <param name="startString">开始字符串</param>
        /// <param name="endString">结束字符串</param>
        /// <returns>返回符合要求的区间字符串(包含开始字符串和结束字符串)</returns>
        public static string SearchString(string sourceString, string startString, string endString)
        {
            if (string.IsNullOrEmpty(sourceString)) return string.Empty;
            if (string.IsNullOrEmpty(startString)) throw new ArgumentNullException("startString");
            if (string.IsNullOrEmpty(endString)) throw new ArgumentNullException("endString");

            int startIndex = sourceString.IndexOf(startString);
            if (startIndex == -1) throw new Exception("找不到开始字符串！");

            int endIndex = sourceString.IndexOf(endString, startIndex + startString.Length);
            if (endIndex == -1) throw new Exception("找不到结束字符串！");

            return sourceString.Substring(startIndex, endIndex - startIndex + endString.Length);
        }
    }
}
